package com.ruoyi.baseData.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ruoyi.baseData.domain.Process;
import com.ruoyi.baseData.domain.Shift;
import com.ruoyi.baseData.domain.dto.ShiftDto;
import com.ruoyi.baseData.domain.vo.ShiftVo;
import com.ruoyi.baseData.mapper.ShiftMapper;
import com.ruoyi.baseData.service.ShiftService;
import com.ruoyi.common.core.exception.GlobalException;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.common.core.utils.sql.QueryHelp;
import com.ruoyi.common.security.utils.SecurityUtils;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 *
 */
@Service
public class ShiftServiceImpl implements ShiftService {
    @Resource
    ShiftMapper shiftMapper;

    @Override
    public Page<ShiftVo> getShiftByPage(ShiftDto pageEntity) {
        MPJLambdaWrapper<Shift> wrapper = new MPJLambdaWrapper<Shift>()
                .selectAll(Shift.class)//查询user表全部字段
                //记的测试阶段
                .eq(Shift::getDelFlag, 0)
                .orderByDesc(Shift::getCreateTime);
        wrapper = QueryHelp.getPredicateEx(wrapper, Shift.class, pageEntity);
        Page<ShiftVo> userList = shiftMapper.selectJoinPage(new Page<>(pageEntity.getCurrentPage(), pageEntity.getPageSize()),   ShiftVo.class, wrapper);
        return userList;
    }

    @Override
    public void addShift(ShiftDto testDto) throws ParseException {
        String userName = SecurityUtils.getUsername();

        if(null == testDto.getShiftTime()) {
            throw new GlobalException("该班次的时间没有输入");
        }

        String startTimeStr  = "";
        String endTimeStr = "";
        String[] parts = testDto.getShiftTime().split(",");
        startTimeStr = parts[0];
        endTimeStr = parts[1];


        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
        Date startTime = formatter.parse(startTimeStr);
        Date endTime = formatter.parse(endTimeStr);

        Long differenceInMilliSeconds = endTime.getTime() - startTime.getTime();
        Long differenceInSeconds = TimeUnit.MILLISECONDS.toSeconds(differenceInMilliSeconds)/60;




//        LocalDateTime dateTime1 = LocalDateTime.of(startTime);
//
//        Duration duration = Duration.between(startTime, endTime);

        MPJLambdaWrapper<Shift> wrapper = new MPJLambdaWrapper<Shift>()
                .eq(Shift::getCode, testDto.getCode())
                .ne(Shift::getDelFlag, 1);
        Shift material = shiftMapper.selectOne(wrapper);
        if(null != material) {
            throw new GlobalException("该material已经存在");
        }
        Shift test2 = new Shift();
        BeanUtils.copyProperties(testDto, test2, new String[]{"id"});
        test2.setStartTime(startTime);
        test2.setEndTime(endTime);
        test2.setDelFlag(false);
        test2.setCreateBy(userName);
        test2.setWorkingTime(differenceInSeconds.intValue());
        test2.setCreateTime(new Date());
        shiftMapper.insert(test2);
    }

    @Override
    public void updateShift(ShiftDto testDto) {
        MPJLambdaWrapper<Shift> wrapper = new MPJLambdaWrapper<Shift>()
                .eq(Shift::getCode, testDto.getCode());
        Shift material = shiftMapper.selectOne(wrapper);
        if(null == material) {
            throw new GlobalException("该Test不存在");
        }
        BeanUtils.copyProperties(testDto, material);
        material.setCreateBy("admin");
        material.setCreateTime(new Date());
        shiftMapper.updateById(material);
    }

    @Override
    public void delShift(ShiftDto testDto) {
        Shift material = shiftMapper.selectById(testDto.getId());
        if(null == material) {
            throw new GlobalException("该Test不存在");
        }
        material.setDelFlag(true);
        shiftMapper.updateById(material);
    }
}




